/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */


var common = null;

Array.prototype.remove = function (val) {
    var index = this.indexOf(val);
    if (index > -1) {
        this.splice(index, 1);
    }
};

$(function () {
    common = {
        ajax: function (url, data, cb) {
            $.ajax({
                url: url,
                type: "post",
                dataType: "json",
                data: data,
                success: cb
            });
        },
        htmlAjax: function (url, data, cb) {
            $.ajax({
                url: url,
                type: "post",
                dataType: "html",
                data: data,
                success: cb
            });
        },
        redirect: function (url) {
            window.location.href = url;
        },
        reload: function () {
            window.location.reload();
        },
        //提示框
        alert: function(msg,fn){
        	layer.alert(msg, {icon: 6,closeBtn: false},function(index){
        		layer.close(index);
        		if(typeof(fn) != "undefined"){
            		fn();
        		}
        	});
        },
        //确认框
        confirm: function(msg,fn){
        	layer.confirm(msg, {
        		icon: 6,
    		  	btn: ['确定','取消'],
    		  	closeBtn: false
    		}, function(index){
    		  	layer.close(index);
    		  	fn();
    		});
        },
        //提示层
        msg: function(msg,time){
        	if(typeof(time) == "undefined"){
        		time = 3000;
    		}
        	return layer.msg(msg,{time: time});
        },
        //弹出新窗口
        open: function(titile,html,width,height) {
        	if(typeof(width) == "undefined"){
        		width = 800;
    		}
        	if(typeof(height) == "undefined"){
        		height = 550;
    		}
        	layer.open({
              	type: 1,
              	title: titile,
              	area: [width+'px', height+'px'], //宽高
              	content: html
            });
		},
		//加载进度
        loading: function(){
        	return layer.load(2);
        },
        //关闭加载进度
        closeLoading: function (win) {
        	if (win == "" || win == null || typeof(win) == "undefined") {
        		layer.closeAll();
			}else{
				layer.close(win);
			}
        },
        inArray: function (value, array) {
            var status = false;
            for (var key in array) {
                if (array[key] == value) {
                    status = true;
                }
            }
            return status;
        },
        isEmptyArray: function (obj) {
            if (JSON.stringify(obj) === '[]') {
                return true;
            }
            return false;
        },
        /**
    	 * 倒计时
    	 * @param waitTime:倒计时时间
    	 * @param cb1:倒计时结束后的回调
    	 * @param cb2:倒计时过程中的回调
    	 */
        timeDown: function (waitTime,cb1,cb2){
    		if (waitTime == 0) {
				if(typeof(cb1) == "function"){
					cb1();
	        	}
	        } else {
	            waitTime--;
	            if(typeof(cb2) == "function"){
	            	cb2(waitTime);
	            }
	           	setTimeout(function() {
	           		common.timeDown(waitTime,cb1,cb2);
	           	},1000);
	        }
    	},
    	//验证数字,key 元素id,digits 小数位数
    	isNumber: function (key,digits){
    		var numValue = $.trim($("#"+key).val());
    		var reg = new RegExp("^[1-9]+[0-9]*(\\.\\d{0,"+digits+"})?$","gim");
    	    if(!reg.test(numValue) && numValue!=""){
    			return false;
    		}
    	    return true;
    	},
    	//验证手机号码,key 元素id
    	isMobile: function (key){
    		var mobileNumber = $.trim($("#"+key).val());
    		if(mobileNumber == ""){
    			return false;
    		}
    	    var reg = /^((((13|14|15|18|17)[0-9]{1}))+\d{8})$/;
    	    if(!reg.test(mobileNumber)){
    			return false;
    		}
    	    return true;
    	},
    	//回车事件
    	enterCall: function (fn,id){
    		var obj = document;
    		if (typeof(id) != "undefined") {
    			obj = "#"+id;
			}
    		$(obj).keydown(function(e) {
    			if (e.keyCode == 13) {
    				fn();
    		    }
    		});
    	},
    	//图片路径处理
		imageFile: function(url, wid, hei) {
			var width = wid || 100;
			var height = hei || 100;
			return (url + '').replace(/(\w+)\.(\w+)$/g, '$1' + '_' + width + '_' + height + '.$2');
		},
    };
});